package com.xxw.yygh.user.service.impl;

import com.xxw.yygh.client.DictFeignClient;
import com.xxw.yygh.common.utils.JwtHelper;
import com.xxw.yygh.model.user.Patient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxw.yygh.user.dao.PatientDao;

import com.xxw.yygh.user.service.PatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 就诊人表(Patient)表服务实现类
 *
 * @author makejava
 * @since 2023-03-03 16:03:23
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientDao, Patient> implements PatientService {

    @Resource
    private DictFeignClient dictFeignClient;


    @Override
    public List<Patient> findAll(String token) {
        Long userId = JwtHelper.getUserId(token);

        QueryWrapper<Patient> patientQueryWrapper = new QueryWrapper<>();
        patientQueryWrapper.eq("user_id",userId);
        List<Patient> patients = baseMapper.selectList(patientQueryWrapper);

        patients.stream().forEach(item->{
            this.packagePatient(item);
        });

        return patients;
    }

    @Override
    public Patient detail(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packagePatient(patient);
        return patient;
    }

    @Override
    public List<Patient> selectList(QueryWrapper<Patient> patientQueryWrapper) {

        List<Patient> patients = baseMapper.selectList(patientQueryWrapper);
        patients.stream().forEach(item->{
            this.packagePatient(item);
        });
        return patients;
    }

    private void packagePatient(Patient item) {
        //远程调用字典微服务
        item.getParam().put("certificatesTypeString",dictFeignClient.getNameByValue(Long.parseLong(item.getCertificatesType())));
        String provinceString = dictFeignClient.getNameByValue(Long.parseLong(item.getProvinceCode()));
        String cityString = dictFeignClient.getNameByValue(Long.parseLong(item.getCityCode()));
        String disctrictString = dictFeignClient.getNameByValue(Long.parseLong(item.getDistrictCode()));
        item.getParam().put("provinceString",provinceString);
        item.getParam().put("cityString",cityString);
        item.getParam().put("districtString",disctrictString);

        item.getParam().put("fullAddress",provinceString+cityString+disctrictString+item.getAddress());

    }
}

